package com.carefree.system.common.utils;

import com.carefree.common.core.Result;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName:WebUtils
 * @Description: 用于Spring Security异常处理类：AccessDeniedHandler、AuthenticationEntryPoint
 * @Author Tengbin tengbin234@163.com
 */
public class WebUtils {


    /**
     * 将指定字符串响应到客户端上
     *
     * @param response 渲染对象
     * @param outStr   响应到客户端上的字符串
     * @return
     */
    public static void renderString(HttpServletResponse response, Result result) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            mapper.writeValue(response.getWriter(), result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
